SpringBoot整合Dubbo3.0基础配置 | 您所在的位置:网站首页 › thread starter › SpringBoot整合Dubbo3.0基础配置 |
SpringBoot整合Dubbo3.0基础配置(dubbo-spring-boot-starter)
一、说明
众所周知,阿里早已把dubbo捐赠给了Apache,现在dubbo由Apache在维护更新,dubbo也已经成了Apache下的顶级项目。所以本demo项目所依赖的坐标是Apache官方最新的3.0.4坐标。 org.apache.dubbo dubbo-spring-boot-starter 3.0.4下面的这些依赖 不是我们考虑的依赖 com.alibaba.spring.boot dubbo-spring-boot-starter 2.0.0 io.dubbo.springboot spring-boot-starter-dubbo 1.0.0SpringBoot是我们喜欢的项目配置框架,因为不用写xml,使用配置文件来约定我们的配置。本demo项目采用父子模块结构,解决了SpringBoot项目父子模块依赖的问题,可作为其他类似结构项目的参考。我们本着依赖最小化原则,不需要的依赖我们不引入。本demo项目dubbo配置属性只是基础配置,如需更多功能配置,请自行扩展。 本demo项目采用zookeeper为注册中心,默认你已经安装好zookeeper服务器,并已经启动。 二、父模块创建父模块只是一个pom工程,用来管理依赖 选择Spring Initializr 选择Maven POM 这一步我们不勾选依赖 这就是建好的父模块结构,只有pom.xml文件 这种做的目的就是为了让spring-boot-starter-parent帮我们构建、管理、打包项目,还不会生成一些不需要的文件夹和文件,省去自己去做这些的麻烦,避免项目打包后运行出错。当然你也可以不使用标签,不依赖spring-boot-starter-parent,有些教程让依赖spring-boot-dependencies,这样你就要自己解决打包的问题。 下面我们删除不需要的依赖,建立我们自己的依赖管理 4.0.0 pom org.springframework.boot spring-boot-starter-parent 2.3.1.RELEASE com.example dubbo 0.0.1-SNAPSHOT dubbo Demo project for Spring Boot to Dubbo 1.8 3.0.4 4.2.0 org.apache.dubbo dubbo-spring-boot-starter ${dubbo-boot.version} org.apache.curator curator-x-discovery ${zkclient.version} 三、创建interface模块interface模块用来管理暴露的接口和实体类。在父模块下新建Module,直接选择maven方式创建 pom.xml文件只需引入lombok依赖 dubbo com.example 0.0.1-SNAPSHOT 4.0.0 dubbo-interface org.projectlombok lombok provided org.apache.maven.plugins maven-compiler-plugin 3.1 ${java.version} ${java.version} ${project.build.sourceEncoding}User.java: @Data @NoArgsConstructor @AllArgsConstructor public class User implements Serializable { private static final long serialVersionUID = 8728327146677888239L; private Integer userId; private String userName; }UserService.java: public interface UserService { User getByUserId(Integer userId); }interface模块结构 和interface模块一样,采用maven方式,在父模块下新建module pom.xml文件如下: dubbo com.example 0.0.1-SNAPSHOT 4.0.0 dubbo-provider org.springframework.boot spring-boot-starter org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-x-discovery com.example dubbo-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin配置文件: # 这里的配置属性只是基础配置,如需更多功能配置,请自行扩展 dubbo: application: name: dubbo-provider registry: address: zookeeper://127.0.0.1:2181 protocol: name: dubbo port: 20880UserServiceImpl: /** * 3.0x后dubbo @Service 为了和 spring的 @Service区分,改为了 @DubboService,相应的 @Reference * 也改为了 @DubboReference */ @DubboService public class UserServiceImpl implements UserService { // 模拟数据 private static final List USERS = Arrays.asList( new User(1, "张三"), new User(2, "李四") ); @Override public User getByUserId(Integer userId) { for (User user : USERS) { if (user.getUserId().equals(userId)) { return user; } } return null; } }为了provider工程能运行,我们需添加启动类 DubboProviderApplication.java: @EnableDubbo @SpringBootApplication public class DubboProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboProviderApplication.class, args); } }provider模块结构 consumer模块使用web工程对外提供访问,用来测试从provider获取数据,展示到web页面上。创建方式和上面的2个模块一样,采用maven方式,在父模块下新建module。 pom.xml文件 dubbo com.example 0.0.1-SNAPSHOT 4.0.0 dubbo-consumer org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter org.apache.curator curator-x-discovery com.example dubbo-interface 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin配置文件: dubbo: application: name: dubbo-consumer registry: address: zookeeper://127.0.0.1:2181 protocol: name: dubbo port: 20880 # 由于zookeeper启动时要占用8080端口,我们要显示指定端口,要不然会端口冲突 server: port: 8081UserController.java: @RestController public class UserController { @DubboReference private UserService userService; @GetMapping("/user/{userId}") public ResponseEntity getByUserId(@PathVariable("userId") Integer userId) { System.out.println("userId = " + userId); User user = userService.getByUserId(userId); System.out.println("user = " + user); return ResponseEntity.ok(userService.getByUserId(userId)); } }DubboConsumerApplication.java: @SpringBootApplication public class DubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboConsumerApplication.class, args); } }consumer模块结构 整个项目的结构 在浏览器看测试结果 项目打包上线才是我们最终目的,所以我们需要测试打包后的效果 打包后的项目,只需要执行provider和consumer就可以了
关闭idea启动的工程,运行jar包 运行provider: 运行consumer: 浏览器查看效果:
完结。 |
CopyRight 2018-2019 实验室设备网 版权所有 |